12. Lesson Review

In this lesson we learned…

What kinds of things can be bad in scripts?

  • Inefficient loops
  • Inefficient references to game objects
  • Update, FixedUpdate, and Await methods
  • Leaving scripts running when they don’t need to be

Tip: Remember that any code in the update method gets run every frame. If we’re running at 60 FPS, that’s 60 times per second. That’s a lot!

What is an infinite loop?

An infinite loop is a loop that never reaches its end condition. These can be very bad for performance and can cause apps to crash entirely. For example:

for(int i = 0; i >= 0; i++) {
     // i is always going to be greater than or equal to 0 because it’s set to start at 0. Uh oh!
     // The exception here, is if we have something inside this loop to make i something less than 0.
}

while (true) {
    // True is always true, so it’s always going to run. Eek!
}

How can we optimize our references to objects in our scripts?

In general, we want to define as much as we can at the top of our script and reuse those variables later in our script’s methods. If we can’t define something ahead of time, we need to try to define it in the quickest way possible.

For example, GameObject.Find() is a useful method, but it can be expensive in terms of performance. Especially if used in an Update method.

void Update() {
     GameObject obj = GameObject.Find(“MyObject”);
}

One way to optimize this would be using the GameObject.Find() method in the start method. That way it would be only called once.

void Start () {
     GameObject obj = GameObject.Find(“MyObject”);
}

However, we can optimize further by adding a public reference to a gameobject at the top of our script and assigning it in the inspector.

public GameObject obj;

What is the difference between FixedUpdate and Update?

FixedUpdate runs every frame similar to Update, but it runs in sync with the physics engine. Update runs as fast as possible to update things visually.

All of our scripting that has to do with physics (like adding forces to an object’s rigidbody) should go into FixedUpdate rather than Update. This will lead to better physics simulations and increases consistency.

What is the difference between the Awake and Start methods?

Awake is similar to Start in that they are both called only once. However, the key difference is that Awake is called when a script instance is being loaded and is used to initialize variables or game state before the game starts. Awake gets called before the start function.

Why should we handle our own “garbage collecting”?

Garbage collecting is the process of removing out of use objects. Setting objects disabled doesn’t remove it from our scene. It simply hides it from users. We can use the destroy method to completely remove objects from the scene.

// Removes the object now.
Destroy(this.gameObject);

// Removes the object in 3 seconds.
Destroy(this.gameObject, 3);

Tip: Be sure to remove the reference to objects that you destroy from any lists they may be in. If you don’t you’ll likely see some NullReferenceExceptions show up in your console.